<template>
  <div>
    <header>
      <div class="swipers">
        <swiper :modules="modules" :pagination="{ clickable: true }">
          <swiper-slide v-for="(item, index) in swiperlist" :key="index">
            <img :src="item.imgurl" alt="" />
          </swiper-slide>
        </swiper>
      </div>
      <div class="goodsName">
        <h1>{{ this.goodsinfo.content }}</h1>
        <div>相加首选，查干十足，县宏村耐泡的大红茶</div>
      </div>
      <div class="goodsPrice">
        <div class="oprice">
          <span>￥</span>
          <b>{{ this.goodsinfo.price }}</b>
        </div>
        <div class="pprice">
          <span>价格</span>
          <b>￥139</b>
        </div>
      </div>
    </header>

    <footer>
      <div class="addCart" @click="addCart">加入购物车</div>
      <div>立即购买</div>
    </footer>
  </div>
</template>

<script>
import { Pagination } from "swiper";
import { Swiper, SwiperSlide } from "vue-awesome-swiper";

// import swiper module styles
import "swiper/css";
import "swiper/css/pagination";
import qs from "qs";
export default {
  name: "Detail",
  components: {
    Swiper,
    SwiperSlide,
  },
  data() {
    return {
      modules: [Pagination],
      goodsinfo: [],
      swiperlist: [
        { id: 1, imgurl: "./images/swiper1.jpeg" },
        { id: 2, imgurl: "./images/swiper2.jpeg" },
        { id: 3, imgurl: "./images/swiper3.jpeg" },
      ],
      userGoods: {
        token: "",
        goodsID: 1,
      },
    };
  },
  created() {
    this.getGoods();
  },
  methods: {
    getGoods() {
      //创建URLSeachParams对象，使用对象来获取地址栏的参数
      let goodsID = this.$route.params;
      this.$http.post("/detail", qs.stringify(goodsID)).then((res) => {
        // console.log(res.data);
        this.goodsinfo = res.data;
        // console.log(this.goodsinfo.goodsID);
      });
    },

    //加入购物车
    addCart() {
      // console.log(this.goodsinfo.goodsID);
      this.userGoods.token = window.sessionStorage.getItem("token");
      if (this.userGoods.token == null)
        return this.$message.error("请先登录！");
      this.userGoods.goodsID = this.$route.params.id;
      // console.log(this.userGoods.token);
      this.$http.post("/cart", qs.stringify(this.userGoods)).then((res) => {
        console.log(res);
      });
      this.$router.push("/cart/" + this.userGoods.token);
    },
  },
};
</script>

<style lang="less" scoped>
.goodsName {
  border-bottom: 1px solid #ccc;
  padding-bottom: 13px;
  h1 {
    font-size: 18px;
    padding: 0 10px;
  }
  div {
    font-size: 16px;
    padding: 0 10px;
    color: #aaa;
  }
}
.goodsPrice {
  padding: 10px 10px;
  .oprice {
    color: red;
  }
  .pprice {
    color: #aaa;
    font-size: 16px;
    text-decoration: line-through;
  }
}
footer {
  position: fixed;
  bottom: 0;
  left: 0;
  display: flex;
  justify-content: space-around;
  align-items: center;
  justify-items: center;
  border-top: 1px solid #ccc;
  width: 100%;
  height: 40px;
  font-size: 15px;
  div {
    width: 50%;
    text-align: center;
    background-color: red;
    line-height: 40px;
    color: #fff;
  }
  .addCart {
    background-color: #cfa006;
  }
}
.swipers {
  img {
    height: 300px;
  }
}
</style>
